home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 10 / AACD 10.iso / AACD / Online / SpeakFreely / src / libdes / speed.c < prev    next >
C/C++ Source or Header  |  2000-05-18  |  8KB  |  311 lines

  1. /* lib/des/speed.c */
  2. /* Copyright (C) 1995 Eric Young (eay@mincom.oz.au)
  3.  * All rights reserved.
  4.  * 
  5.  * This file is part of an SSL implementation written
  6.  * by Eric Young (eay@mincom.oz.au).
  7.  * The implementation was written so as to conform with Netscapes SSL
  8.  * specification.  This library and applications are
  9.  * FREE FOR COMMERCIAL AND NON-COMMERCIAL USE
  10.  * as long as the following conditions are aheared to.
  11.  * 
  12.  * Copyright remains Eric Young's, and as such any Copyright notices in
  13.  * the code are not to be removed.  If this code is used in a product,
  14.  * Eric Young should be given attribution as the author of the parts used.
  15.  * This can be in the form of a textual message at program startup or
  16.  * in documentation (online or textual) provided with the package.
  17.  * 
  18.  * Redistribution and use in source and binary forms, with or without
  19.  * modification, are permitted provided that the following conditions
  20.  * are met:
  21.  * 1. Redistributions of source code must retain the copyright
  22.  *    notice, this list of conditions and the following disclaimer.
  23.  * 2. Redistributions in binary form must reproduce the above copyright
  24.  *    notice, this list of conditions and the following disclaimer in the
  25.  *    documentation and/or other materials provided with the distribution.
  26.  * 3. All advertising materials mentioning features or use of this software
  27.  *    must display the following acknowledgement:
  28.  *    This product includes software developed by Eric Young (eay@mincom.oz.au)
  29.  * 
  30.  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  31.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  32.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  34.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  35.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  36.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  37.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  38.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  39.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  40.  * SUCH DAMAGE.
  41.  * 
  42.  * The licence and distribution terms for any publically available version or
  43.  * derivative of this code cannot be changed.  i.e. this code cannot simply be
  44.  * copied and put under another distribution licence
  45.  * [including the GNU Public Licence.]
  46.  */
  47.  
  48. /* 11-Sep-92 Andrew Daviel   Support for Silicon Graphics IRIX added */
  49. /* 06-Apr-92 Luke Brennan    Support for VMS and add extra signal calls */
  50.  
  51. #ifndef MSDOS
  52. #define TIMES
  53. #endif
  54.  
  55. #include <stdio.h>
  56. #ifndef MSDOS
  57. #include <unistd.h>
  58. #else
  59. #include <io.h>
  60. #endif
  61. #include <signal.h>
  62. #ifndef VMS
  63. #ifndef _IRIX
  64. #include <time.h>
  65. #endif
  66. #ifdef TIMES
  67. #include <sys/types.h>
  68. #include <sys/times.h>
  69. #endif
  70. #else /* VMS */
  71. #include <types.h>
  72. struct tms {
  73.     time_t tms_utime;
  74.     time_t tms_stime;
  75.     time_t tms_uchild;    /* I dunno...  */
  76.     time_t tms_uchildsys;    /* so these names are a guess :-) */
  77.     }
  78. #endif
  79. #ifndef TIMES
  80. #include <sys/timeb.h>
  81. #endif
  82.  
  83. #ifdef sun
  84. #include <limits.h>
  85. #include <sys/param.h>
  86. #endif
  87.  
  88. #include "des.h"
  89.  
  90. /* The following if from times(3) man page.  It may need to be changed */
  91. #ifndef HZ
  92. #ifndef CLK_TCK
  93. #ifndef VMS
  94. #define HZ    100.0
  95. #else /* VMS */
  96. #define HZ    100.0
  97. #endif
  98. #else /* CLK_TCK */
  99. #define HZ ((double)CLK_TCK)
  100. #endif
  101. #endif
  102.  
  103. #define BUFSIZE    ((long)1024*8)
  104. long run=0;
  105.  
  106. #ifdef PROTO
  107. double Time_F(int s);
  108. #else
  109. double Time_F();
  110. #endif
  111.  
  112. #ifdef SIGALRM
  113. #if defined(__STDC__) || defined(sgi)
  114. #define SIGRETTYPE void
  115. #else
  116. #define SIGRETTYPE int
  117. #endif
  118.  
  119. #ifdef PROTO
  120. SIGRETTYPE sig_done(int sig);
  121. #else
  122. SIGRETTYPE sig_done();
  123. #endif
  124.  
  125. SIGRETTYPE sig_done(sig)
  126. int sig;
  127.     {
  128.     signal(SIGALRM,sig_done);
  129.     run=0;
  130. #ifdef LINT
  131.     sig=sig;
  132. #endif
  133.     }
  134. #endif
  135.  
  136. #define START    0
  137. #define STOP    1
  138.  
  139. double Time_F(s)
  140. int s;
  141.     {
  142.     double ret;
  143. #ifdef TIMES
  144.     static struct tms tstart,tend;
  145.  
  146.     if (s == START)
  147.         {
  148.         times(&tstart);
  149.         return(0);
  150.         }
  151.     else
  152.         {
  153.         times(&tend);
  154.         ret=((double)(tend.tms_utime-tstart.tms_utime))/HZ;
  155.         return((ret == 0.0)?1e-6:ret);
  156.         }
  157. #else /* !times() */
  158.     static struct timeb tstart,tend;
  159.     long i;
  160.  
  161.     if (s == START)
  162.         {
  163.         ftime(&tstart);
  164.         return(0);
  165.         }
  166.     else
  167.         {
  168.         ftime(&tend);
  169.         i=(long)tend.millitm-(long)tstart.millitm;
  170.         ret=((double)(tend.time-tstart.time))+((double)i)/1000.0;
  171.         return((ret == 0.0)?1e-6:ret);
  172.         }
  173. #endif
  174.     }
  175.  
  176. int main(argc,argv)
  177. int argc;
  178. char **argv;
  179.     {
  180.     long count;
  181.     static unsigned char buf[BUFSIZE];
  182.     static des_cblock key ={0x12,0x34,0x56,0x78,0x9a,0xbc,0xde,0xf0};
  183.     static des_cblock key2={0x34,0x56,0x78,0x9a,0xbc,0xde,0xf0,0x12};
  184.     static des_cblock key3={0x56,0x78,0x9a,0xbc,0xde,0xf0,0x12,0x34};
  185.     des_key_schedule sch,sch2,sch3;
  186.     double a,b,c,d,e;
  187. #ifndef SIGALARM
  188.     long ca,cb,cc,cd,ce;
  189. #endif
  190.  
  191. #ifndef TIMES
  192.     printf("To get the most acurate results, try to run this\n");
  193.     printf("program when this computer is idle.\n");
  194. #endif
  195.  
  196.     des_set_key((C_Block *)key2,sch2);
  197.     des_set_key((C_Block *)key3,sch3);
  198.  
  199. #ifndef SIGALRM
  200.     printf("First we calculate the aproximate speed ...\n");
  201.     des_set_key((C_Block *)key,sch);
  202.     count=10;
  203.     do    {
  204.         long i;
  205.  
  206.         count*=2;
  207.         Time_F(START);
  208.         for (i=count; i; i--)
  209.             des_ecb_encrypt((C_Block *)buf,(C_Block *)buf,
  210.                 &(sch[0]),DES_ENCRYPT);
  211.         d=Time_F(STOP);
  212.         } while (d < 3.0);
  213.     ca=count;
  214.     cb=count*3;
  215.     cc=count*3*8/BUFSIZE+1;
  216.     cd=count*8/BUFSIZE+1;
  217.     ce=count/20+1;
  218.     printf("Doing set_key %ld times\n",ca);
  219. #define COND(d)    (count != (d))
  220. #define COUNT(d) (d)
  221. #else
  222. #define COND(c)    (run)
  223. #define COUNT(d) (count)
  224.     signal(SIGALRM,sig_done);
  225.     printf("Doing set_key for 10 seconds\n");
  226.     alarm(10);
  227. #endif
  228.  
  229.     Time_F(START);
  230.     for (count=0,run=1; COND(ca); count++)
  231.         des_set_key((C_Block *)key,sch);
  232.     d=Time_F(STOP);
  233.     printf("%ld set_key's in %.2f seconds\n",count,d);
  234.     a=((double)COUNT(ca))/d;
  235.  
  236. #ifdef SIGALRM
  237.     printf("Doing des_ecb_encrypt's for 10 seconds\n");
  238.     alarm(10);
  239. #else
  240.     printf("Doing des_ecb_encrypt %ld times\n",cb);
  241. #endif
  242.     Time_F(START);
  243.     for (count=0,run=1; COND(cb); count++)
  244.         des_ecb_encrypt((C_Block *)buf,(C_Block *)buf,
  245.             &(sch[0]),DES_ENCRYPT);
  246.     d=Time_F(STOP);
  247.     printf("%ld des_ecb_encrypt's in %.2f second\n",count,d);
  248.     b=((double)COUNT(cb)*8)/d;
  249.  
  250. #ifdef SIGALRM
  251.     printf("Doing des_cbc_encrypt on %ld byte blocks for 10 seconds\n",
  252.         BUFSIZE);
  253.     alarm(10);
  254. #else
  255.     printf("Doing des_cbc_encrypt %ld times on %ld byte blocks\n",cc,
  256.         BUFSIZE);
  257. #endif
  258.     Time_F(START);
  259.     for (count=0,run=1; COND(cc); count++)
  260.         des_cbc_encrypt((C_Block *)buf,(C_Block *)buf,BUFSIZE,&(sch[0]),
  261.             (C_Block *)&(key[0]),DES_ENCRYPT);
  262.     d=Time_F(STOP);
  263.     printf("%ld des_cbc_encrypt's of %ld byte blocks in %.2f second\n",
  264.         count,BUFSIZE,d);
  265.     c=((double)COUNT(cc)*BUFSIZE)/d;
  266.  
  267. #ifdef SIGALRM
  268.     printf("Doing des_ede_cbc_encrypt on %ld byte blocks for 10 seconds\n",
  269.         BUFSIZE);
  270.     alarm(10);
  271. #else
  272.     printf("Doing des_ede_cbc_encrypt %ld times on %ld byte blocks\n",cd,
  273.         BUFSIZE);
  274. #endif
  275.     Time_F(START);
  276.     for (count=0,run=1; COND(cd); count++)
  277.         des_ede3_cbc_encrypt((C_Block *)buf,(C_Block *)buf,BUFSIZE,
  278.             &(sch[0]),
  279.             &(sch2[0]),
  280.             &(sch3[0]),
  281.             (C_Block *)&(key[0]),
  282.             DES_ENCRYPT);
  283.     d=Time_F(STOP);
  284.     printf("%ld des_ede_cbc_encrypt's of %ld byte blocks in %.2f second\n",
  285.         count,BUFSIZE,d);
  286.     d=((double)COUNT(cd)*BUFSIZE)/d;
  287.  
  288. #ifdef SIGALRM
  289.     printf("Doing crypt for 10 seconds\n");
  290.     alarm(10);
  291. #else
  292.     printf("Doing crypt %ld times\n",ce);
  293. #endif
  294.     Time_F(START);
  295.     for (count=0,run=1; COND(ce); count++)
  296.         crypt("testing1","ef");
  297.     e=Time_F(STOP);
  298.     printf("%ld crypts in %.2f second\n",count,e);
  299.     e=((double)COUNT(ce))/e;
  300.  
  301.     printf("set_key            per sec = %12.2f (%5.1fuS)\n",a,1.0e6/a);
  302.     printf("DES ecb bytes      per sec = %12.2f (%5.1fuS)\n",b,8.0e6/b);
  303.     printf("DES cbc bytes      per sec = %12.2f (%5.1fuS)\n",c,8.0e6/c);
  304.     printf("DES ede cbc bytes  per sec = %12.2f (%5.1fuS)\n",d,8.0e6/d);
  305.     printf("crypt              per sec = %12.2f (%5.1fuS)\n",e,1.0e6/e);
  306.     exit(0);
  307. #ifdef LINT
  308.     return(0);
  309. #endif
  310.     }
  311.